package com.morizhang.merchanmanager.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.morizhang.merchanmanager.api.ApiResult;
import com.morizhang.merchanmanager.api.IService;
import com.morizhang.merchanmanager.api.bean.MerChanManagerException;
import com.morizhang.merchanmanager.api.model.CommodityLocationModel;
import com.morizhang.merchanmanager.api.model.StockModel;
import com.morizhang.merchanmanager.api.model.UnlocateCommodityModel;
import com.morizhang.merchanmanager.api.plugins.BasicWareManage;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

@ServiceAnnotation(name = "LocateCommodityService")
public class LocateCommodityService implements IService {
    @Autowired
    private BasicWareManage basicWareManage;
    @Override
    public ApiResult callService(JSONObject requestParams) throws Exception {
        String callMethodName = requestParams.getString("requestMethod");
        Method callMethod = this.getClass().getMethod(callMethodName, JSONObject.class);
        ApiResult result = (ApiResult)callMethod.invoke(this, requestParams);
        result.setSuccess(true);
        return result;
    }

    public ApiResult queryUnlocateCommodity(JSONObject requestParams) {
        JSONObject requestParamsJSON = requestParams.getJSONObject("data");
        List<StockModel> searchResult = basicWareManage.queryAllCommodityUnlocation();
        Iterator<StockModel> i = searchResult.iterator();
        JSONArray resultJSON = new JSONArray();
        while (i.hasNext()) {
            UnlocateCommodityModel unlocateCommodityModel = new UnlocateCommodityModel();
            StockModel next = i.next();
            unlocateCommodityModel.setByStockModel(next);
            unlocateCommodityModel.setByCommodityDataModel(basicWareManage.queryCommodity(next.getBarCode()));
            String productTime = basicWareManage.queryCommodityStorage(next).getProductTime();

            if (productTime == null || productTime.trim().isEmpty()) {
                unlocateCommodityModel.setTimeRemain("不存在");
            } else {
                Calendar now = Calendar.getInstance();
                long nowTimeStamp = now.getTimeInMillis();
                long productTimeLong = Long.parseLong(productTime);
                long betweenMillis = nowTimeStamp - productTimeLong;
                unlocateCommodityModel.setTimeRemain(String.valueOf(betweenMillis / 86400000));
            }

            resultJSON.add(JSONObject.parseObject(JSON.toJSONString(unlocateCommodityModel)));
        }
        ApiResult apiResult = new ApiResult();
        apiResult.setData(resultJSON);
        return apiResult;
    }

    public ApiResult locateCommodity(JSONObject requestParams) throws MerChanManagerException {
        JSONObject requestParamsJSON = requestParams.getJSONObject("data");
        StockModel unlocatedCommodity = JSON.toJavaObject(requestParamsJSON, StockModel.class);
        String warehouseShelfNo = requestParamsJSON.getString("warehouseShelfNo");
        basicWareManage.locateCommodity(unlocatedCommodity, warehouseShelfNo);
        return new ApiResult();
    }

    public ApiResult unlocateCommodity(JSONObject requestParams) throws MerChanManagerException {
        JSONObject requestParamsJSON = requestParams.getJSONObject("data");
        CommodityLocationModel unlocatedCommodity = JSON.toJavaObject(requestParamsJSON, CommodityLocationModel.class);
        basicWareManage.unlocateCommodity(unlocatedCommodity);
        return new ApiResult();
    }

    public ApiResult moveCommodityInWarehouse(JSONObject requestParams) throws MerChanManagerException {
        JSONObject requestParamsJSON = requestParams.getJSONObject("data");
        CommodityLocationModel moveCommodityInfo = JSON.toJavaObject(requestParamsJSON, CommodityLocationModel.class);
        basicWareManage.moveCommodityInWarehouse(moveCommodityInfo);
        return new ApiResult();
    }

    public ApiResult moveCommodityToShop(JSONObject requestParams) throws Exception {
        JSONObject requestParamsJSON = requestParams.getJSONObject("data");
        CommodityLocationModel moveCommodityInfo = JSON.toJavaObject(requestParamsJSON, CommodityLocationModel.class);
        basicWareManage.moveCommodityToShop(moveCommodityInfo);
        return new ApiResult();
    }
}
